package com.ddwl.gate.util.boHuan;

import com.ddwl.gate.constant.MobileConfigConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.security.MessageDigest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Author hemiaojun
 * @Date 2020/9/24 15:32
 */
@Slf4j
@Component
public class BoHuanRechargeUtil {

    public static void addSign(HashMap<String,String> map) throws Exception {
        map.put("szAgentId", MobileConfigConstant.USER_ID);
        String sign = generateSignature(map);
        map.put("szVerifyString", sign);
        map.put("szFormat", "JSON");
    }

    public static void queryBalanceAddSign(HashMap<String,String> map) throws Exception{
        map.put("szAgentId", MobileConfigConstant.USER_ID);
        String signStr = "szAgentId=" + MobileConfigConstant.USER_ID + "&szKey=" + MobileConfigConstant.KEY;
        log.info("queryBalanceAddSign signStr [{}]",signStr);
        String sign = MD5(signStr);
        log.info("queryBalanceAddSign szVerifyString [{}]",sign);
        map.put("szVerifyString", sign);
        map.put("szFormat", "JSON");
    }

    public static void submitOrderAddSign(HashMap<String,String> map) throws Exception{
        map.put("szAgentId", MobileConfigConstant.USER_ID);
        String signStr = "szAgentId=" + MobileConfigConstant.USER_ID +"&szOrderId="+map.get("szOrderId")+"&szPhoneNum="+map.get("szPhoneNum")
                +"&nMoney="+map.get("nMoney")+"&nSortType="+map.get("nSortType")+"&nProductClass="+map.get("nProductClass")+"&nProductType="+map.get("nProductType")
                +"&szTimeStamp="+map.get("szTimeStamp")+"&szKey=" + MobileConfigConstant.KEY;
        log.info("submitOrderAddSign signStr [{}]",signStr);
        String sign = MD5(signStr);
        log.info("submitOrderAddSign szVerifyString [{}]",sign);
        map.put("szVerifyString", sign);
        map.put("szFormat", "JSON");
    }

    public static void queryOrderAddSign(HashMap<String,String> map) throws Exception{
        map.put("szAgentId", MobileConfigConstant.USER_ID);
        String signStr = "szAgentId=" + MobileConfigConstant.USER_ID +"&szOrderId="+map.get("szOrderId")+"&szKey=" + MobileConfigConstant.KEY;
        log.info("queryOrderAddSign signStr [{}]",signStr);
        String sign = MD5(signStr);
        log.info("submitOrderAddSign szVerifyString [{}]",sign);
        map.put("szVerifyString", sign);
        map.put("szFormat", "JSON");
    }

    public static String notifyAddSign(Map<String,String> map) throws Exception{
        String signStr = "szAgentId=" + map.get("szAgentId") +"&szOrderId="+map.get("szOrderId")+"&szPhoneNum="+map.get("szPhoneNum")
                +"&nDemo="+map.get("nDemo")+"&fSalePrice="+map.get("fSalePrice")+"&nFlag="+map.get("nFlag")+"&szKey=" + MobileConfigConstant.KEY;
        log.info("notifyAddSign signStr [{}]",signStr);
        String sign = MD5(signStr);
        log.info("notifyAddSign szVerifyString [{}]",sign);
        return sign;
    }

    /**
     * 生成签名.
     * @param data 待签名数据
     * @return 签名
     */
    public static String generateSignature(final Map<String, String> data) throws Exception {
        Set<String> keySet = data.keySet();
        String[] keyArray = keySet.toArray(new String[keySet.size()]);
        Arrays.sort(keyArray);
        StringBuilder sb = new StringBuilder();
        for (String k : keyArray) {
            if (k.equals(MobileConfigConstant.FIELD_SIGN) || k.equals(MobileConfigConstant.FIELD_NOTIFY_URL) || k.equals(MobileConfigConstant.FIELD_RTN_MSG)) {
                continue;
            }
            if (data.get(k).trim().length() > 0) {
                sb.append(data.get(k).trim());
            }
        }
        sb.append(MobileConfigConstant.KEY);
        log.info("bohuan sign param [{}]",sb.toString());
        return MD5(sb.toString());
    }

    /**
     * 生成 MD5
     *
     * @param data 待处理数据
     * @return MD5结果
     */
    public static String MD5(String data) throws Exception {
        java.security.MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] array = md.digest(data.getBytes("UTF-8"));
        StringBuilder sb = new StringBuilder();
        for (byte item : array) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    }

    public static Boolean CheckSign(Map<String, String> map) throws Exception{
        String sign = notifyAddSign(map);
        return sign.equals(map.get("szVerifyString"));
    }
}
